<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\CollegeCarousel;
use Common\Models\CollegeCollect;
use Common\Models\CollegeColumn;
use Common\Models\CollegeShare;
use Common\Models\CollegeStudy;
use Common\Models\CollegeZan;
use Common\Models\User;

class CollegeController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}

    /**
     * 商学院列表
     */
    public function listAction(){
        $college_column = new CollegeColumn();
        $list = $college_column->find_list();
        $user = new User();
        $user_info = $user->find_by_firstId($this->user_info['id']);
        $collegeCarousel = new CollegeCarousel();
        $new_list = [];
        foreach($list as $key=>$value){
            $new_list[$key]['cid']=$value['id'];
            $new_list[$key]['name']=$value['name'];
            $new_list[$key]['type']=$value['type'];
            $new_list[$key]['short_name']=$value['short_name'];
            $new_list[$key]['is_lock'] = ($user_info['level_id'] >= $value['level_id']) ? 0 : 1;
            $carousel = $collegeCarousel->find_list($value['id'],0,1);
            $carousel = Common::transition_array($carousel);
            $new_list[$key]['carousel']=$this->statiac($carousel);
        }
        $data['command']=$collegeCarousel->find_list(0,0,1);
        $data['list']=$new_list;
        $this->ok($data);
    }


    //获取分类
    public function get_cateAction(){
        $college_cate = new CollegeColumn();
        $getlist = $college_cate->find_list();
        $params_list[0] = ['id'=>0,'name'=>'全部分类'];
        if($getlist){
            $getlist = array_merge($params_list,$getlist);
        }
        $this->ok($getlist);
    }

    public function get_carousel_listAction(){
        if(!isset($this->params['type']) || empty($this->params['type'])){
            $this->params['type']='';
        }
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->params['cid']='';
        }
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page']=1;
        }
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $this->params['page_size']=10;
        }
        $college_carousel = new CollegeCarousel();
        $columns="id,title,cover,cid,cateid,type,sharecount,zancount,studycount,collectcount,created_at";
        $get_list = $college_carousel->get_list($this->params,$columns);
        $get_list = $this->statiac($get_list,$this->user_info['id']);
        $this->ok($get_list);
    }


    //获取具体的内容
    public function collegedetailAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'id不能为空');
        }
        $collegeCarousel = new CollegeCarousel();
        $find_one = $collegeCarousel->find_by_id($this->params['id']);
        if(!$find_one){
            $this->fail(-1,'没有找到该详情，或已被删除');
        }
        
        $Study = new CollegeStudy();
        $studycount = $Study->find_count($this->params['id']);     //学习量
        if($find_one['type'] == 3){
            $tips=($studycount>10000) ? $studycount/10000 : $studycount;
            $tips=$tips.'人已收听';
        }else{
            $tips=($studycount>10000) ? $studycount/10000 : $studycount;
            $tips=$tips.'人已学习';
        }
        $find_one['studycount'] =$tips;
        $find_one['h5url']=Common::domain().'/webapp/college?id='.$find_one['id'].'&user_id='.$this->user_info['id'];

        $zan = new CollegeZan();
        $zan_tip = $zan->find_count($this->params['id']) + $find_one['zancount'];
        $find_one['zancount'] = "点赞{$zan_tip}次";  //分享量
        $is_zan = $zan->is_zan($this->user_info['id'],$this->params['id']);
        $find_one['is_zan'] = $is_zan>0 ? 1 : 0;//是否点赞
        $Share = new CollegeShare();
        $share_tip = $Share->find_count($this->params['id']) + $find_one['sharecount'];
        $find_one['sharecount'] = '分享'.$share_tip;  //分享量
        $studyDb = new CollegeStudy();
        $studyDb->isstudy($this->user_info['id'],$this->params['id']);
        $this->ok($find_one);
    }
    
    
    
    
    /**
     * 点赞
     */
    public function my_zanAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'查看id不能为空');
        }

        $CarousDb = new CollegeCarousel();
        $carousel_find_one = $CarousDb->find_by_id($this->params['id']);
        if(!$carousel_find_one){
            $this->fail(-1,'没有找到记录行');
        }
        $zan = new CollegeZan();
        $is_zan = $zan->is_zan($this->user_info['id'],$this->params['id']);
        if($is_zan){
            $this->fail(-1,'您已经赞过了');
        }

        $zan->user_id = $this->user_info['id'];
        $zan->carousel_id = $this->params['id'];
        $zan->created_at = date('Y-m-d H:i:s');
        $zan->updated_at = date('Y-m-d H:i:s');
        if($zan->save()){
            $count_tips = $zan->find_count($this->params['id'])+$carousel_find_one['zancount'];
            $data['count'] = '点赞'.$count_tips.'次';
            $this->ok($data);
        }
        $this->fail(-1,'点赞失败');
    }
    
    
    
    /**
     * 分享
     */
    public function my_shareAction(){
        if(!isset($this->params['id']) || empty($this->params['id'])){
            $this->fail(-1,'查看id不能为空');
        }
        $share = new CollegeShare();
        $is_share = $share->is_share($this->user_info['id'],$this->params['id']);
        if($is_share){
            $this->fail(-1,'您已经分享过了');
        }
        $share->user_id = $this->user_info['id'];
        $share->carousel_id = $this->params['id'];
        $share->created_at = date('Y-m-d H:i:s');
        $share->updated_at = date('Y-m-d H:i:s');
        if($share->save()){
            $data['count'] = '分享'.$share->find_count($this->params['id']).'次';
            $this->ok($data);
        }
        $this->fail(-1,'点赞失败');
    }

    
    
    /**
     * 获取统计学习 分享 是否收藏
     * @param $carousel_list    对象
     * @param $user_id
     * @param int $is_share     判断是否需要显示收藏
     * @return mixed
     */
    public function statiac($carousel_list,$user_id=0,$is_collect=0){
        foreach($carousel_list as $key=>$value){
            $Study = new CollegeStudy();
            $studycount = $Study->find_count($value['id']);     //学习量
            $studycount = $studycount + $value['studycount'];
            if($value['type'] == 3){
                $tips=($studycount>10000) ? Common::number_randtwo($studycount/10000).'万人已收听' : $studycount.'人已收听';
            }else{
                $tips=($studycount>10000) ? Common::number_randtwo($studycount/10000).'万人已学习' : $studycount.'人已学习';
            }
            
            $carousel_list[$key]['h5url']=Common::domain().'/webapp/college?id='.$value['id'].'&user_id='.$user_id;
            $created_at =strtotime($value['created_at']);
            $carousel_list[$key]['created_at'] =date('Y-m-d',$created_at);
            $carousel_list[$key]['studycount'] =$tips;
            $zan = new CollegeZan();
            $zan_tip = ($zan->find_count($value['id'])+$value['zancount']).'人已赞';


            $carousel_list[$key]['zanCount'] = $zan_tip;  //分享量
            $Share = new CollegeShare();
            $sharecount = $Share->find_count($value['id'])+$value['sharecount'];
            $share_tip = $sharecount>10000 ? Common::number_randtwo($sharecount/10000).'万人已分享' : $sharecount.'人已分享';
            $carousel_list[$key]['sharecount'] = $share_tip;  //分享量
            if($is_collect) {
                $collect = new CollegeCollect();
                $user_is_collect = $zan->is_zan($user_id,$value['id']);//$collect->find_count($value['id'], $user_id);     //个人是否收藏
                $carousel_list[$key]['is_collect'] = $user_is_collect ? 1 : 0;
                $zancount = (int)($zan->find_count($value['id'])+$value['zancount']);
                $carousel_list[$key]['collectcount'] = $zancount > 10000 ? Common::number_randtwo($zancount/10000).'万' : $zancount.'';
            }
        }
        return $carousel_list;
    }
}
